package kontrol;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import database.DALException;
import database.MySQLRaavareDAO;
import database.RaavareDTO;

/**
 * Servlet implementation class Rawmat
 */
@WebServlet({ "/Rawmat", "/Rawmat/*" })
public class Rawmat extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Rawmat() {
		super();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		if (request.getRequestURI().endsWith("Rawmat/AddRaw")) {
			request.getRequestDispatcher("/WEB-INF/rawmat/createraw.jsp").forward(request, response);
			
		} else if (request.getRequestURI().endsWith("Rawmat/ChangeRaw")) {
			request.getRequestDispatcher("/WEB-INF/rawmat/changeraw.jsp").forward(request, response);
			
		} else if (request.getRequestURI().endsWith("Rawmat/ShowRaw")) {
			formatRawMatList(request);
			request.getRequestDispatcher("/WEB-INF/rawmat/showraw.jsp").forward(request, response);
			
		} else if (request.getRequestURI().endsWith("Rawmat/RawmatHome")) {
			request.getRequestDispatcher("/WEB-INF/rawmat/rawmathome.jsp").forward(request, response);
		}
		return;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		int raavareId = 0;
		
		String raavareNavn = null;
		String leverandoer = null;
		MySQLRaavareDAO dao = null;
		RaavareDTO rawDTO = null;
		RaavareDTO dto = null;

		String data = request.getParameter("newRaavare");

		if (data != null) {
			if ((request.getParameter("raavareNavn") != null)
					&& (request.getParameter("leverandoer") != null));
				
			raavareNavn = request.getParameter("raavareNavn");
			leverandoer = request.getParameter("leverandoer");

			dto = new RaavareDTO(raavareNavn, leverandoer);

			try {
				dao = new MySQLRaavareDAO();
				dao.createRaavare(dto);
			} catch (DALException e1) {
				request.setAttribute("error",
						"Could not create object in database");
				e1.printStackTrace();
			}
			request.setAttribute("message", "Recipe succesful created!");
			request.getRequestDispatcher("/WEB-INF/uploadSuccessful.jsp").forward(
					request, response);
		}
		String rawmatchange = request.getParameter("editRaavare");

		if (rawmatchange != null) {

			String message = "";
			try {
				dao = new MySQLRaavareDAO();
			} catch (DALException e2) {
				e2.printStackTrace();
			}


			if (request.getParameter("raavareId") != null) {

				try {
					raavareId = Integer.valueOf(request.getParameter("raavareId"));
				}
				catch (NumberFormatException e) {
					e.printStackTrace();
					message = message + "NumberFormatException";
					request.setAttribute("message", message);
				}
				message = message + "raavareId acceped";
			}
			else {
				message = message + "raavareId is not defined";
			}

			if (request.getParameter("raavareNavn") != null) {

				try {
					raavareNavn = request.getParameter("raavareNavn"); 
				}
				catch (NumberFormatException e) {
					e.printStackTrace();
					message = message + "NumberFormatExceptiom";
					request.setAttribute("message", message);
				}
				message = message + "raavareNavn accepted";
			}
			else {
				message = message + "raavaraNavn is not defined";
			}


			if (request.getParameter("leverandoer") != null) {

				try {
					leverandoer = request.getParameter("leverandoer");
				}
				catch (NumberFormatException e) {
					e.printStackTrace();
					message = message  + "NumberFormatException";
					request.setAttribute("message", message);
				}
				message = message + "leverandoer accepted";
			}
			else {
				message = message + "leverandoer is not defined";
			}

			try {
				rawDTO = dao.getRaavare(Integer.valueOf(request.getParameter("raavareId")));
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (DALException e) {
				e.printStackTrace();
			}

			if (rawDTO != null) {
				rawDTO.setRaavareNavn(raavareNavn);
				rawDTO.setLeverandoer(leverandoer);

				try {
					dao.updateRaavare(rawDTO);
					request.setAttribute("message","succesfull!");
				} catch (NumberFormatException e1) {
					e1.printStackTrace();
					request.setAttribute("message", "NumberFormatException");

				} catch (DALException e1) {
					e1.printStackTrace();
				}
			}

			else {
				message = message + "raavare does not exist";
				request.setAttribute("message", message);
			}

			request.getRequestDispatcher("/WEB-INF/uploadSuccessful.jsp").forward(
					request, response);
		}
	}
	
	
	
	public void formatRawMatList(HttpServletRequest request)
			throws ServletException, IOException {

		MySQLRaavareDAO rawMatDAO = null;
		try {
			rawMatDAO = new MySQLRaavareDAO();
			request.setAttribute("count", rawMatDAO.getCount());
		} catch (DALException e) {
			e.printStackTrace();
			request.setAttribute("count", 0);
		}
		if (((Integer) request.getAttribute("count")) < 10) {
			try {
				List<RaavareDTO> liste = rawMatDAO.getRaavareList();
				String results = "<table border=\"1\"> <tr> <th>RawMat Id</th> <th>RawMat Name</th> <th>Supplier</th> </tr>";
				for (int i = 0; i < liste.size(); i++) {
					results += "<tr> <td>" + liste.get(i).getRaavareId()
							+ "</td><td> " + liste.get(i).getRaavareNavn()
							+ "</td><td> " + liste.get(i).getLeverandoer()
							+ "<br>" + "</td> </tr>";

				}
				results += "</table>";
				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		} else {
			if (request.getAttribute("page") == null) {
				request.setAttribute("page", 0);
			}
			if (request.getParameter("p") != null) {
				request.setAttribute("page",
						Integer.parseInt(request.getParameter("p")));
			}
			int curPage = (Integer) request.getAttribute("page");
			try {
				List<RaavareDTO> liste = rawMatDAO.getRaavareList(curPage, 10);
				String results = "<table border=\"1\"> <tr> <th>RawMat Id</th> <th>RawMat Name</th> <th>Supplier</th> </tr>";
				for (int i = 0; i < liste.size(); i++) {

					results += "<tr> <td>" + liste.get(i).getRaavareId()
							+ "</td><td> " + liste.get(i).getRaavareNavn()
							+ "</td><td> " + liste.get(i).getLeverandoer()
							+ "<br>" + "</td> </tr>";
				}
				results += "</table>";
				if (curPage > 9) {
					results += "<a href=\"?p=" + (curPage - 10)
							+ "\">Prev</a>&nbsp;&nbsp;&nbsp;";
				}
				if ((((Integer) request.getAttribute("count")) - 10) > curPage) {
					results += "<a href=\"?p=" + (curPage + 10) + "\">Next</a>";
				}

				request.setAttribute("results", results);
			} catch (DALException e) {

				e.printStackTrace();
			}
		}
	}	
}
